#include "bsp_system.h"

volatile static uint32_t delay_count;

void system_config(void)
{
    nvic_priority_group_set(NVIC_PRIGROUP_PRE0_SUB4);

    if (SysTick_Config(SystemCoreClock / 1000U / 1000U))
    {
        while (1)
            ;
    }
    NVIC_SetPriority(SysTick_IRQn, 0x00U);
}

void delay_sw_us(uint32_t count)
{
    uint32_t sw_count = count;
    while (sw_count != 0)
    {
        sw_count--;
        for (uint32_t i = 0; i < 19; i++)
            ;
    }
}

void delay_sw_ms(uint32_t count)
{
    uint32_t sw_count = count;
    while (sw_count != 0)
    {
        sw_count--;
        for (uint32_t i = 0; i < 24200; i++)
            ;
    }
}

void delay_us(uint32_t count)
{
    delay_count = count;

    while (0U != delay_count)
        ;
}

void delay_ms(uint32_t count)
{
    delay_count = count * 1000; // ms 比 us 多延时1000倍

    while (0U != delay_count)
        ;
}

void SysTick_Handler(void)
{
    if (0U != delay_count)
    {
        delay_count--;
    }
}
